<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <base href="<%=request.getContextPath()%>/">
    <link rel="stylesheet" href="static/layui/css/layui.css">
    <link rel="stylesheet" href="static/css/base.css">
    <script src="static/js/jquery.js"></script>
    <script src="static/js/ajax.js"></script>
    <script src="static/layui/layui.js"></script>
</head>
<body>
<form id="editForm" class="layui-form" style="margin: 10px">
    <input type="hidden" id="id" name="id" value="${category.id}">
    <div class="layui-form-item">
        <label class="layui-form-label" for="name"><span class="red">*</span>分类名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" id="name" placeholder="请输入分类名" class="layui-input"
                   value="${category.name}"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" for="profile">介绍</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" name="profile" id="profile"
                      placeholder="请输入备注">${category.profile}</textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" for="status"><span class="red">*</span>状态</label>
        <div class="layui-input-block">
            <select name="status" id="status">
                <option value="1">可用</option>
                <option value="2">禁用</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-filter="submitBtn" lay-submit>提交</button>
        </div>
    </div>
</form>
</body>
<script>
    const {form} = layui;
    let status = "${category.status}";
    if (status) {
        $("#status").val(status);
        form.render();
    }
    $(function () {
        form.on("submit(submitBtn)", function (data) {
            ajax.post("category/update", data.field).then(() => {
                layer.alert(`${data.field.id?"修改":"添加"}成功！`, {icon: 6}, function () {
                    closeWin();
                    parent.reloadTable();
                })
            })
            return false;
        })
    })

    function closeWin() {
        const index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
        parent.layer.close(index);
    }
</script>
</html>

